package com.millstein.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.millstein.model.BannerDO;
import com.millstein.mapper.BannerMapper;
import com.millstein.service.BannerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.millstein.vo.BannerVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author millstein
 * @since 2021-09-16
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, BannerDO> implements BannerService {

    /**
     * 分页查询轮播图信息
     *
     * @param page 当前页
     * @param size 每页记录数
     * @return
     */
    @Override
    public Page<BannerVO> pageBanner(int page, int size) {
        Page<BannerDO> doPage = new Page<>(page, size);
        QueryWrapper<BannerDO> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("weight");
        this.page(doPage, wrapper);

        List<BannerVO> voList = doPage.getRecords().stream().map(BannerVO::new).collect(Collectors.toList());
        Page<BannerVO> voPage = new Page<>();
        BeanUtils.copyProperties(doPage, voPage);
        voPage.setRecords(voList);
        return voPage;
    }
}
